package com.xuelianx.fingerlib;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;

import com.xuelianx.fingerlib.base.BaseFingerprint;

/**
 * author : hejunming
 * e-mail : 925379434@qq.com
 * date   : 2018/11/15 19:15
 */
public class FingerprintScanHelper {

    private FingerprintIdentify mFingerprintIdentify;
    private Callback mCallback;
    private Context mContext;
    private static final int MAX_AVAILABLE_TIMES = 3;

    public interface Callback {
        void onSuccess();

        void onNotFingerprint();

        void onDeviceNotSupport();

        /**
         * @param code 1 指纹接口错误；2 验证失败，可再试；3 指纹识别次数用完，转输入密码；4 指纹验证太过频繁，转输入密码
         */
        void onError(int code);
    }

    public void setCallBack(Callback mCallBack) {
        this.mCallback = mCallBack;
    }

    public FingerprintScanHelper(Context context) {
        this.mContext = context;
        mFingerprintIdentify = new FingerprintIdentify(mContext, new BaseFingerprint.FingerprintIdentifyExceptionListener() {
            @Override
            public void onCatchException(Throwable exception) {
                if (mCallback != null) {
                    mCallback.onError(1);
                }
//                    Toast.makeText(getActivity(), exception.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }

        });
        if (mFingerprintIdentify != null && mFingerprintIdentify.isHardwareEnable()) {
            if (!mFingerprintIdentify.isRegisteredFingerprint()) {
                if (mCallback != null) {
                    mCallback.onNotFingerprint();
                }
            }
        } else {
            if (mCallback != null) {
                mCallback.onDeviceNotSupport();
            }
        }
    }

    public void start() {

        mFingerprintIdentify.startIdentify(MAX_AVAILABLE_TIMES, new BaseFingerprint.FingerprintIdentifyListener() {
            @Override
            public void onSucceed() {
                if (mCallback != null) {
                    mCallback.onSuccess();
                }
            }

            @Override
            public void onNotMatch(int availableTimes) {
                if (availableTimes == 0) {
                    if (mCallback != null) {
                        mCallback.onError(3);
                    }
                } else {
                    if (mCallback != null) {
                        mCallback.onError(2);
                    }
                }
            }

            @Override
            public void onFailed(boolean isDeviceLocked) {
                if (isDeviceLocked) {
                } else {
                }
                if (mCallback != null) {
                    mCallback.onError(3);
                }
            }

            @Override
            public void onStartFailedByDeviceLocked() {
                if (mCallback != null) {
                    mCallback.onError(4);
                }
            }
        });
    }

    public void shake(View v) {
        Animation shake = AnimationUtils.loadAnimation(mContext, R.anim.shake);
        v.startAnimation(shake);
    }

    public void cancelIdentify() {
        mFingerprintIdentify.cancelIdentify();
    }
}
